package cn.wolfcode.interceptor;

import cn.wolfcode.domain.Result;
import cn.wolfcode.util.JWTUtils;
import com.alibaba.fastjson.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String token = request.getHeader("token");
        try {
            JWTUtils.parserToken(token);
            System.out.println("放行！");
            return true;
        } catch (Exception e) {
            System.out.println("不让过");
            Result not = Result.error("NOT");
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().print(JSON.toJSONString(not));
            return false;
        }
    }
}
